/*     */ package cn.zzuisa.news.repository.dao;

/*     */
/*     */ import cn.zzuisa.news.entity.Page;
/*     */ import cn.zzuisa.news.entity.User;
/*     */ import cn.zzuisa.news.form.RegisterForm;
/*     */ import cn.zzuisa.news.form.UserEditForm;
/*     */ import cn.zzuisa.news.repository.UserRepository;
/*     */ import cn.zzuisa.news.util.JDBCUtil;
/*     */ import java.sql.Connection;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.sql.Statement;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;

/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */ public class UserDAO/*     */ implements UserRepository
/*     */ {
	Connection conn = null;
	Statement statement = null;
	ResultSet resultSet = null;
	PreparedStatement prepareStatement = null;
	PreparedStatement preparedStatement = null;

	/*     */ public User login(String username, String password) {
		/* 33 */ conn = JDBCUtil.getConnection();
		/* 34 */ prepareStatement = null;
		/* 35 */ resultSet = null;
		/* 36 */ String sql = "select id,username,password,nickname,email,point,role,state,createtime from `news_user` where username = ? and password = ?";
		/*     */ try {
			/* 38 */ prepareStatement = conn.prepareStatement(sql);
			/* 39 */ prepareStatement.setString(1, username);
			/* 40 */ prepareStatement.setString(2, password);
			/* 41 */ resultSet = prepareStatement.executeQuery();
			/* 42 */ User user = new User();
			/* 43 */ if (resultSet.next()) {
				/* 44 */ user.setId(Integer.valueOf(resultSet.getInt("id")));
				/* 45 */ user.setUsername(resultSet.getString("username"));
				/* 46 */ user.setPassword(resultSet.getString("password"));
				/* 47 */ user.setNickname(resultSet.getString("nickname"));
				/* 48 */ user.setEmail(resultSet.getString("email"));
				/* 49 */ user.setPoint(Integer.valueOf(resultSet.getInt("point")));
				/* 50 */ user.setRole(Integer.valueOf(resultSet.getInt("role")));
				/* 51 */ user.setState(Integer.valueOf(resultSet.getInt("state")));
				/* 52 */ user.setCreatetime(resultSet.getTimestamp("createtime"));
				/* 53 */ return user;
				/*     */ }
			/*     */
			/* 56 */ } catch (SQLException e) {
			/*     */
			/* 58 */ e.printStackTrace();
			/*     */ } finally {
			/*     */ try {
				/* 61 */ JDBCUtil.close(conn, prepareStatement, resultSet);
				/* 62 */ } catch (Exception e) {
				/*     */
				/* 64 */ e.printStackTrace();
				/*     */ }
			/*     */ }
		/*     */
		/* 68 */ return null;
		/*     */ }

	/*     */
	/*     */
	/*     */ public <T> boolean register(T user) {
		/* 73 */ conn = JDBCUtil.getConnection();
		/* 74 */ prepareStatement = null;
		/* 75 */ int flag = 0;
		/* 76 */ String sql = "insert into `news_user` (username,password,nickname,email) values (?,?,?,?)";
		/*     */ try {
			/* 78 */ prepareStatement = conn.prepareStatement(sql);
			/* 79 */ prepareStatement.setString(1, ((RegisterForm) user).getUsername());
			/* 80 */ prepareStatement.setString(2, ((RegisterForm) user).getPassword());
			/* 81 */ prepareStatement.setString(3, ((RegisterForm) user).getNickname());
			/* 82 */ prepareStatement.setString(4, ((RegisterForm) user).getEmail());
			/* 83 */ flag = prepareStatement.executeUpdate();
			/*     */ }
		/* 85 */ catch (SQLException e) {
			/*     */
			/* 87 */ e.printStackTrace();
			/*     */ } finally {
			/*     */ try {
				/* 90 */ JDBCUtil.close(conn, prepareStatement);
				/* 91 */ } catch (Exception e) {
				/*     */
				/* 93 */ e.printStackTrace();
				/*     */ }
			/*     */ }
		/* 96 */ return (flag > 0);
		/*     */ }

	/*     */
	/*     */
	/*     */
	/*     */ public User findById(Integer id) {
		/* 102 */ conn = JDBCUtil.getConnection();
		/* 103 */ statement = null;
		/* 104 */ resultSet = null;
		/* 105 */ int flag = 0;
		/* 106 */ String sql = "select id,username,password,nickname,email,point,role,state,createtime from `news_user` where id="
				+
				/* 107 */ id;
		/* 108 */ User user = new User();
		/*     */ try {
			/* 110 */ statement = conn.createStatement();
			/* 111 */ resultSet = statement.executeQuery(sql);
			/* 112 */ if (resultSet.next()) {
				/* 113 */ user.setNickname(resultSet.getString("nickname"));
				/* 114 */ user.setPoint(Integer.valueOf(resultSet.getInt("point")));
				/*     */ }
			/* 116 */ } catch (SQLException e) {
			/*     */
			/* 118 */ e.printStackTrace();
			/*     */ } finally {
			/*     */ try {
				/* 121 */ JDBCUtil.close(conn, statement, resultSet);
				/* 122 */ } catch (Exception e) {
				/*     */
				/* 124 */ e.printStackTrace();
				/*     */ }
			/*     */ }
		/* 127 */ return user;
		/*     */ }

	/*     */
	/*     */
	/*     */
	/*     */ public int addpoint(Integer id) {
		/* 133 */ conn = JDBCUtil.getConnection();
		/* 134 */ statement = null;
		/* 135 */ resultSet = null;
		/* 136 */ int flag = 0;
		/* 137 */ String sql = "UPDATE `news_user` SET point=point+11 where id = " + id;
		/*     */ try {
			/* 139 */ statement = conn.createStatement();
			/* 140 */ flag = statement.executeUpdate(sql);
			/* 141 */ } catch (SQLException e) {
			/*     */
			/* 143 */ e.printStackTrace();
			/*     */ } finally {
			/*     */ try {
				/* 146 */ JDBCUtil.close(conn, statement, resultSet);
				/* 147 */ } catch (Exception e) {
				/*     */
				/* 149 */ e.printStackTrace();
				/*     */ }
			/*     */ }
		/* 152 */ return flag;
		/*     */ }

	/*     */
	/*     */
	/*     */
	/*     */ public User findByUsername(String username) {
		/* 158 */ conn = JDBCUtil.getConnection();
		/* 159 */ preparedStatement = null;
		/* 160 */ resultSet = null;
		/* 161 */ int flag = 0;
		/* 162 */ String sql = "select * from news_user where `username` = ?";
		/* 163 */ User user = null;
		/*     */ try {
			/* 165 */ preparedStatement = conn.prepareStatement(sql);
			/* 166 */ preparedStatement.setString(1, username);
			/* 167 */ resultSet = preparedStatement.executeQuery();
			/* 168 */ if (resultSet.next()) {
				/* 169 */ user = new User();
				/* 170 */ user.setId(Integer.valueOf(resultSet.getInt("id")));
				/* 171 */ user.setUsername(resultSet.getString("username"));
				/* 172 */ user.setPassword(resultSet.getString("password"));
				/* 173 */ user.setNickname(resultSet.getString("nickname"));
				/* 174 */ user.setEmail(resultSet.getString("email"));
				/* 175 */ user.setPoint(Integer.valueOf(resultSet.getInt("point")));
				/* 176 */ user.setRole(Integer.valueOf(resultSet.getInt("role")));
				/* 177 */ user.setState(Integer.valueOf(resultSet.getInt("state")));
				/* 178 */ user.setCreatetime(resultSet.getTimestamp("createtime"));
				/*     */ }
			/* 180 */ } catch (SQLException e) {
			/*     */
			/* 182 */ e.printStackTrace();
			/*     */ } finally {
			/*     */ try {
				/* 185 */ JDBCUtil.close(conn, preparedStatement, resultSet);
				/* 186 */ } catch (Exception e) {
				/*     */
				/* 188 */ e.printStackTrace();
				/*     */ }
			/*     */ }
		/* 191 */ return user;
		/*     */ }

	/*     */
	/*     */
	/*     */
	/*     */ public boolean edit(UserEditForm userEditForm) {
		/* 197 */ conn = JDBCUtil.getConnection();
		/* 198 */ preparedStatement = null;
		/* 199 */ resultSet = null;
		/* 200 */ int flag = 0;
		/* 201 */ String sql = "UPDATE news_user SET password=? where id = ? and password = ?";
		/*     */ try {
			/* 203 */ preparedStatement = conn.prepareStatement(sql);
			/* 204 */ preparedStatement.setString(1, userEditForm.getNewPassword());
			/* 205 */ preparedStatement.setInt(2, userEditForm.getuId().intValue());
			/* 206 */ preparedStatement.setString(3, userEditForm.getPrePassword());
			/* 207 */ flag = preparedStatement.executeUpdate();
			/* 208 */ } catch (SQLException e) {
			/*     */
			/* 210 */ e.printStackTrace();
			/*     */ } finally {
			/*     */ try {
				/* 213 */ JDBCUtil.close(conn, preparedStatement, resultSet);
				/* 214 */ } catch (Exception e) {
				/*     */
				/* 216 */ e.printStackTrace();
				/*     */ }
			/*     */ }
		/* 219 */ return (flag > 0);
		/*     */ }

	/*     */
	/*     */
	/*     */ public Page pagelist(Integer current, Integer size) {
		/* 224 */ conn = JDBCUtil.getConnection();
		/* 225 */ preparedStatement = null;
		/* 226 */ resultSet = null;
		/* 227 */ Page page = null;
		/* 228 */ String sql1 = "select count(id) from news_user";
		/* 229 */ String sql2 = "select * from news_user order by id desc limit ?,? ";
		/*     */ try {
			/* 231 */ int totalCount = 0;
			/* 232 */ preparedStatement = conn.prepareStatement(sql1);
			/* 233 */ resultSet = preparedStatement.executeQuery();
			/* 234 */ while (resultSet.next()) {
				/* 235 */ totalCount = resultSet.getInt(1);
				/*     */ }
			/* 237 */ preparedStatement = conn.prepareStatement(sql2);
			/* 238 */ preparedStatement.setInt(1, (current.intValue() - 1) * size.intValue());
			/* 239 */ preparedStatement.setInt(2, size.intValue());
			/* 240 */ resultSet = preparedStatement.executeQuery();
			/*     */
			/* 242 */ List<User> users = new ArrayList<User>();
			/* 243 */ while (resultSet.next()) {
				/* 244 */ User user = new User();
				/* 245 */ user.setId(Integer.valueOf(resultSet.getInt("id")));
				/* 246 */ user.setUsername(resultSet.getString("username"));
				/* 247 */ user.setPassword(resultSet.getString("password"));
				/* 248 */ user.setNickname(resultSet.getString("nickname"));
				/* 249 */ user.setEmail(resultSet.getString("email"));
				/* 250 */ user.setPoint(Integer.valueOf(resultSet.getInt("point")));
				/* 251 */ user.setRole(Integer.valueOf(resultSet.getInt("role")));
				/* 252 */ user.setState(Integer.valueOf(resultSet.getInt("state")));
				/* 253 */ user.setCreatetime(resultSet.getTimestamp("createtime"));
				/* 254 */ users.add(user);
				/*     */ }
			/* 256 */ page = new Page(size.intValue(), totalCount);
			/* 257 */ page.setData(users);
			/*     */ }
		/* 259 */ catch (SQLException e) {
			/*     */
			/* 261 */ e.printStackTrace();
			/*     */ } finally {
			/*     */ try {
				/* 264 */ JDBCUtil.close(conn, preparedStatement, resultSet);
				/* 265 */ } catch (Exception e) {
				/*     */
				/* 267 */ e.printStackTrace();
				/*     */ }
			/*     */ }
		/* 270 */ return page;
		/*     */ }

	/*     */
	/*     */
	/*     */
	/*     */ public boolean editState(Integer id, Integer key) {
		/* 276 */ conn = JDBCUtil.getConnection();
		/* 277 */ preparedStatement = null;
		/* 278 */ resultSet = null;
		/* 279 */ int flag = 0;
		/* 280 */ String sql = "UPDATE news_user SET state=? where id = ?";
		/*     */ try {
			/* 282 */ preparedStatement = conn.prepareStatement(sql);
			/* 283 */ preparedStatement.setInt(1, key.intValue());
			/* 284 */ preparedStatement.setInt(2, id.intValue());
			/* 285 */ flag = preparedStatement.executeUpdate();
			/* 286 */ } catch (SQLException e) {
			/*     */
			/* 288 */ e.printStackTrace();
			/*     */ } finally {
			/*     */ try {
				/* 291 */ JDBCUtil.close(conn, preparedStatement, resultSet);
				/* 292 */ } catch (Exception e) {
				/*     */
				/* 294 */ e.printStackTrace();
				/*     */ }
			/*     */ }
		/* 297 */ return (flag > 0);
		/*     */ }
	/*     */ }

/*
 * Location:
 * /Users/frank/Desktop/admin/WEB-INF/classes/!/cn/zzuisa/news/repository/dao/
 * UserDAO.class Java compiler version: 8 (52.0) JD-Core Version: 1.0.7
 */